app.controller("z-salaryApprovalRule-c", ['$scope', '$rootScope', '$z-http', '$sce', 'z-salaryApprovalRule-s', 'z-attendance-s', 'z-edit-menu-s','z-employee-s','z-corporation-s','z-department-s','$filter',
    function ($scope, $rootScope, $zhttp, $sce, Service, AttendanceService, MenuService, EmployeeService, CorporationService, DepartmentService, $filter) {
        $scope.viewModel = {};
        $scope.viewModel.perPage = "10";
        $scope.viewModel.rowCount = "1";
        $scope.viewModel.requestType = "";
        $scope.viewModel.requestRole = "";
        $scope.editModel = {};

        //查询页面名称
        MenuService.getMenuByUrl({url: 'salaryApprovalRule'},
            function (data) {
                $scope.viewModel.menuList = data;
            }, function (error) {
            }
        );

        //加载审批规则数据
        $scope.viewModel.bindData = function(){
            $scope.viewModel.isLoading = true;
            Service.getSalaryApprovalRuleList(
                {"1":  "1"},
                function(data){
                    $scope.viewModel.dataList= data;
                    $scope.viewModel.isLoading = false;
                },function(error) {
                    $scope.viewModel.isLoading = false;
                });
        };
        $scope.viewModel.bindData();

        //加载
        $scope.viewModel.load=function(){
            $scope.viewModel.show=true;
        };

        //关闭加载
        $scope.viewModel.unLoad=function(){
            $scope.viewModel.show=false;

            $scope.editModel.approvalRule={};
        };

        //新增
        $scope.viewModel.add = function(approval){
            if ( $scope.viewModel.dataList && $scope.viewModel.dataList.length > 0 ) {
                $rootScope.alertError("审批规则已经存在！");
                return ;
            }
            $scope.editModel.title="新增工资审批规则";
            $scope.editModel.type = true;
            $scope.editModel.data = {};
            //验证不能为空
            $scope.editForm.oneEmployeeNo.$dirty=false;
            $scope.viewModel.load();
        };

        //修正
        $scope.viewModel.update=function(rule){
            $scope.editModel.title="修改工资审批规则";
            $scope.editModel.type = false;
            $scope.editModel.data=angular.copy(rule);
            $scope.editModel.data.salaryRuleId = rule.salaryRuleId;
            $scope.viewModel.load();
        };

        // 新增保存
        $scope.viewModel.save = function () {
            if ( $scope.viewModel.requiredForm() ) {
                return ;
            }
            $scope.viewModel.isLoading=true;

            var params = $scope.editModel.data;
            if($scope.editModel.type){
                Service.saveSalaryRule(
                    params,
                    function (data) {
                        $scope.viewModel.bindData();
                        $scope.viewModel.isLoading=false;
                        $rootScope.alertInfo("添加成功。");
                        $scope.viewModel.unLoad();
                    },
                    function (error) {
                        $scope.viewModel.isLoading=false;
                        $rootScope.alertError(error.fault.message);
                    }
                )
            } else {
                //params['salaryRuleId'] = $scope.editModel.data.salaryRuleId;
                Service.updateSalaryRule(
                    params,
                    function (data) {
                        $scope.viewModel.bindData();
                        $scope.viewModel.isLoading=false;
                        $rootScope.alertInfo("修改成功。");
                        $scope.viewModel.unLoad();
                    },
                    function (error) {
                        $scope.viewModel.isLoading=false;
                        $rootScope.alertError(error.fault.message);
                    }
                )
            }
        };

        //删除
        $scope.viewModel.del=function(rule){
            $rootScope.alertConfirm("确定删除吗？",function() {
                $scope.viewModel.isLoading=true;
                Service.deleteSalaryRule({
                    salaryRuleId: rule.salaryRuleId
                },function(){
                    $scope.viewModel.bindData();
                    $scope.viewModel.isLoading=false;
                    $rootScope.alertInfo("删除成功。");
                    $scope.viewModel.unLoad();
                },function(error){
                    $scope.viewModel.isLoading=false;
                    $rootScope.alertError(error.fault.message);
                });
            });
        };

        $scope.viewModel.requiredForm = function () {
            var type = false;
            var data = $scope.editModel.data;
            if(data.oneEmployeeNo == null){
                $scope.editForm.oneEmployeeNo.$dirty=true;
                $scope.editForm.oneEmployeeNo.$invalid=true;
                type=true;
            }else{
                type=false;
            }
            return type;
        };

        /*   人员选择   */
        $scope.viewEmployeeModel={};
        $scope.viewEmployeeModel.perPage = "10";
        $scope.viewEmployeeModel.rowCount = "1";
        // 人员选择按钮
        $scope.editModel.chooseUpde = function(typo){
            $scope.viewEmployeeModel.current = typo;
            $scope.viewEmployeeModel.show=true;
            $scope.viewEmployeeModel.title="员工选择";
            $scope.viewEmployeeModel.employeeIsSeparated=null;
            $scope.viewEmployeeModel.regionType=null;
            $scope.viewEmployeeModel.employeeNo=null;
            $scope.viewEmployeeModel.roleName=null;
            $scope.viewEmployeeModel.departmentList=[];
            $scope.viewEmployeeModel.corporationList=[];
            $scope.viewEmployeeModel.employeeList=[];
            $scope.viewEmployeeModel.level=null;
            $(".multiselect-selected-text").text("无");
            //下拉多选框长度
            $scope.multiSelectMaxHeight=300;
            //加载信息
            $scope.viewEmployeeModel.bindSelectRegionTypeData = function(){
                //查询公司
                CorporationService.getCorporationList({
                        fromDate:$scope.viewModel.effectiveDate,
                    },
                    function(data){
                        var site = [];
                        $.each(data, function(index, values) {
                            site.push({
                                label : "("+values.corporationCode+")"+values.corporationName,
                                value : values.corporationCode
                            });
                        });
                        $('#ectCorporationRule').multiselect('dataprovider',site);
                    },function(error) {

                    });
            };
            $scope.viewEmployeeModel.bindSelectRegionTypeData();


            /*******使用为下拉带勾选控件*********/
            //员工级别
            $('#ectlevelRule').multiselect({
                includeSelectAllOption: true,
                enableFiltering: true,
                delimiterText: '; ',
                maxHeight: $scope.multiSelectMaxHeight
            });
            //公司
            $('#ectCorporationRule').multiselect({
                includeSelectAllOption: true,
                enableFiltering: true,
                delimiterText: '; ',
                maxHeight: $scope.multiSelectMaxHeight
            });
            //公司选中事件
            $("#ectCorporationRule").change(function(){
                var value=$(this).val();
                var data={};
                data.corporationCodeList=value;
                data.fromDate = $scope.viewModel.effectiveDate;
                if(value==null){
                    var site = [];
                    $('#ectDepartmentNameRule').multiselect('dataprovider',site);
                    $scope.viewEmployeeModel.departmentList=[];
                }else{
                    //查询部门
                    DepartmentService.getDepartmentListByCorporationCodeList(data,
                        function(data){
                            var site = [];
                            $.each(data, function(index, values) {
                                site.push({
                                    label : "("+values.departmentCode+")"+values.departmentName,
                                    value : values.departmentCode
                                });
                            });
                            $('#ectDepartmentNameRule').multiselect('dataprovider',site);
                        },function(error) {

                        });
                }
            });

            //部门
            $('#ectDepartmentNameRule').multiselect({
                includeSelectAllOption: true,
                enableFiltering: true,
                delimiterText: '; ',
                maxHeight: $scope.multiSelectMaxHeight
            });
            EmployeeService.getEmployeeList({
                    rowCount : $scope.viewEmployeeModel.perPage,
                    perPage : $scope.viewEmployeeModel.rowCount,
                    fromDate:$scope.viewModel.effectiveDate,
                },
                function(data){
                    $scope.viewEmployeeModel.dataEmployeeList = data.employeeBeanList;
                    $scope.viewEmployeeModel.totalData = data.pageTotal;
                    $scope.viewEmployeeModel.isLoading=false;
                },function(error) {
                    $rootScope.alertError(error.fault.message);
                    $scope.viewEmployeeModel.isLoading=false;
                });

        }
        //关闭加载
        $scope.viewEmployeeModel.unLoad=function(){
            $scope.viewEmployeeModel.show=false;
            $scope.editModel.title="新增分配";
        };
        //是否选中
        $scope.viewEmployeeModel.isInAssignmentArr=function(employee){
            // if($scope.editModel.employeeCorporation.employeeId == employee.employeeId){
            //     return true;
            // }
            return false;
        };
        //加载数据
        $scope.viewEmployeeModel.bindData = function(){
            $scope.viewEmployeeModel.employeeList=[];
            if($scope.viewEmployeeModel.employeeNo!=null){
                var employeeNoData = $scope.viewEmployeeModel.employeeNo.split("\n");
                for(var i=0;i<employeeNoData.length;i++){
                    $scope.viewEmployeeModel.employeeList.push(employeeNoData[i]);
                }
            }
            $scope.viewModel.isLoading=true;
            //获取基本信息
            EmployeeService.getEmployeeList({
                    employeeList:$scope.viewEmployeeModel.employeeList,
                    departmentList:$scope.viewEmployeeModel.departmentList,
                    corporationList:$scope.viewEmployeeModel.corporationList,
                    employeeLevelList:$scope.viewEmployeeModel.employeeLevelList,
                    employeeIsSeparated:"0",
                    perPage:$scope.viewEmployeeModel.rowCount,
                    rowCount:$scope.viewEmployeeModel.perPage,
                    fromDate:$scope.viewModel.effectiveDate,
                },
                function(data){
                    $scope.viewEmployeeModel.dataEmployeeList = data.employeeBeanList;
                    $scope.viewEmployeeModel.totalData = data.pageTotal;
                    $scope.viewModel.isLoading=false;
                },function(error) {
                    $scope.viewModel.isLoading=false;
                });
        };
        // 点击选择指定人员按钮
        $scope.viewEmployeeModel.employee = function(employee){
            var typo = $scope.viewEmployeeModel.current;
            $scope.viewModel.show=true;
            $scope.editModel.data = $scope.editModel.data || {};
            // $scope.editModel.data[typo+'EmployeeId'] = employee.employeeId;
            $scope.editModel.data[typo+'EmployeeNo'] = employee.employeeNo;
            $scope.editModel.data[typo+'EmployeeName'] = employee.chineseName;
            $scope.viewEmployeeModel.show=false;
        };
        // 员工清空
        $scope.editModel.chooseDelete = function(typo) {
            $scope.editModel.data = $scope.editModel.data || {};
            var attrs = ['EmployeeNo', 'EmployeeName'];
            attrs.map(function (val) {
                $scope.editModel.data[typo + val] = null;
            });
        };

    }]
);